define(function () {
    'use strict';
    function ctrl($scope, $ionicLoading, $timeout, $state, LocalCacheService, CommonService, WTService) {
        var timer;
        var seconds;
        var par = $scope.param = {};

        function upadateSeconds() {
            if (seconds == 0) {
                $scope.buttonState.checkState = false;
                $scope.buttonState.checkText = '获取验证码';
                seconds = 180;
                return;
            }
            seconds--;
            $scope.buttonState.checkText = seconds + '秒后失效';
            timer = $timeout(function () {
                upadateSeconds();
            }, 1000);
        }

        function init() {
            if(timer){
                $timeout.cancel(timer);
            }
            seconds = 180;
            var user = LocalCacheService.getUser();
            //渠道统计相关
            par.source = CommonService.getSource();

            $scope.mobileOld = {
                mobile: user.mobile_tel,
                vCode: ''
            };
            $scope.buttonState = {
                checkState: false,
                submitState: false,
                checkText: '获取验证码'
            };
            $scope.formEvents = {
                getCode: function () {
                    //查询是否有手机号的渠道信息
                    par.recommendInfos = LocalCacheService.getRecommendInfos();
                    var params2 = {mobile_tel: user.mobile_tel,type:'1',app_id:'gcszt'};
                    $scope.buttonState.checkState = true;
                    WTService.sendMobileCode(params2).then(
                        function () {
                            upadateSeconds();
                        },
                        function (result) {
                            $scope.buttonState.checkState = false;
                            var message=CommonService.getErrorInfo(result);
                            if (message=='参数传入错误'){
                                message='请输入正确的手机号码';
                            }
                            CommonService.showAlert({message: message});
                        }
                    );
                },
                next: function () {
                    // TODO 调用手机验证接口
                    $ionicLoading.show();
                    //验证手机验证码
                    var params1 = {mobile_tel: user.mobile_tel, mobile_code: $scope.mobileOld.vCode,app_id:'gcszt'};
                    WTService.checkMobileCode(params1).then(
                        function (data) {
                            $ionicLoading.hide();
                            if (data.error_info) {
                                CommonService.showAlert({message: data.error_info});
                            } else {
                                $state.go('tab.account-modifyPhoneLast');
                            }
                        },
                        function (result) {
                            $ionicLoading.hide();
                            var message=CommonService.getErrorInfo(result);
                            switch (message){
                                case '参数传入错误' : message='请输入正确的手机号码及验证码';
                                    break;
                                case '未知错误' : message='验证码错误';
                                    break;
                            }
                            CommonService.showAlert({message: message});
                        }
                    );
                }
            }
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
    }

    ctrl.$inject = ['$scope', '$ionicLoading', '$timeout', '$state', 'LocalCacheService', 'CommonService', 'WTService'];
    return ctrl;
});